<template>
    <div class="container">
        <div class="opt-area-02">
            <div class="left-con">
                录入总数: <span>{{countData.fcnum}} 条</span>
            </div>
            <div class="right-con">
                <div class="input-frame">
                    <Input type="text" @on-click="selectlist(1)" placeholder="请输入姓名" v-model="params.name"
                           icon="ios-search"/>
                </div>
                <div class="input-frame">
                    <Input type="text" @on-click="selectlist(1)" placeholder="请输入身份证号" v-model="params.card_no"
                           icon="ios-search"/>
                </div>
                <div class="input-frame" style="width: 164px">
                  <Select v-model="params.area1" placeholder="区/县" filterable @on-change="perChange1(1)">
                    <Option v-for="obj in addressInfo1.list1" :value="obj.company_code" :key="obj.id">{{obj.title}}</Option>
                  </Select>
                </div>
                <div class="input-frame" style="width: 164px">
                  <Select v-model="params.area2" placeholder="街道" filterable @on-change="perChange1(2)">
                    <Option v-for="obj in addressInfo1.list2" :value="obj.company_code" :key="obj.id">{{obj.title}}</Option>
                  </Select>
                </div>
                <div class="input-frame" style="width: 164px">
                  <Select v-model="params.area3" placeholder="社区" filterable>
                    <Option v-for="obj in addressInfo1.list3" :value="obj.company_code" :key="obj.id">{{obj.title}}</Option>
                  </Select>
                </div>
                <Button type="info" size="small" @click="selectlist(1)" style="margin-top: 5px;margin-right:5px">搜索</Button>
                <Button type="success" size="small" @click="register(1)" style="margin-top: 5px">登记</Button>
            </div>
            <div class="right-con">
            </div>
        </div>
        <Table :loading="countData.loading" border :columns="columns2" :data="fcDataList" size="small" ref="table">
            <template slot-scope="{ row }" slot="card_type">
              <span v-if="row.card_type == '1'">身份证</span>
              <span v-if="row.card_type == '2'">军官证</span>
              <span v-if="row.card_type == '3'">护照</span>
              <span v-if="row.card_type == '4'" >港澳通行证</span>
            </template>
            <template slot="action" slot-scope="{ row }">
                <Row style="margin-bottom: 5px; margin-top: 2px">
                  <Button class="table-action-btn" type="info" size="small" @click="showdetail(row, 'info')">详情</Button>
                  <Button class="table-action-btn" type="primary" size="small" @click="personRegister(row)">登记</Button>
                </Row>
            </template>
        </Table>
        <Row v-if="loadData == true">
          <Col class="demo-spin-col" span="8" offset="8">
            <Spin fix>
              <Icon type="ios-loading" size=18 class="demo-spin-icon-load"></Icon>
              <div>Loading</div>
            </Spin>
          </Col>
        </Row>
        <Row type="flex" justify="end" class="page_style">
          <Page :current="params.pageNo" :total="params.totalRecord" show-sizer :page-size="params.pageSize"
                show-total @on-change="pageAction" @on-page-size-change="pageSizeChange"/>
        </Row>
        <Modal v-model="modal3" title="登记情况" width="1000">
          <Form :model="formInfo" label-position="top" :rules="ruleValidate" ref="addBaseForm">
            <div class="title-style">人员基本信息</div>
            <Row :gutter="16">
              <Col span="7">
                <FormItem label="证件号码：" prop="card_no">
                  <Input type="text" v-model="formInfo.card_no" placeholder="请输入证件号码" :disabled="!editFlag"/>
                </FormItem>
              </Col>
              <Col span="2" v-if="editFlag">
                <FormItem label="">
                  <Button type="success" @click="getUserInfo(formInfo.card_no)" style="margin-top: 22px">查询</Button>
                </FormItem>
              </Col>
              <Col span="5">
                <FormItem label="姓名：" prop="name">
                  <Input type="text" v-model="formInfo.name" placeholder="请输入姓名"/>
                </FormItem>
              </Col>
              <Col span="4">
                <FormItem>
                  <template slot="label">
                    <span class="red-star">*</span>性别：
                  </template>
                  <RadioGroup v-model="formInfo.sex">
                    <Radio label="男">男</Radio>
                    <Radio label="女">女</Radio>
                  </RadioGroup>
                </FormItem>
              </Col>
              <Col span="5">
                <FormItem>
                  <template slot="label">
                    <span class="red-star">*</span>证件类型：
                  </template>
                  <Select v-model="formInfo.card_type" placeholder="请选择证件类型"
                          @on-change="ruleValidate.card_no[0].card_type=formInfo.card_type">
                    <Option value="1">身份证</Option>
                    <Option value="2">军官证</Option>
                    <Option value="3">护照</Option>
                    <Option value="4">港澳通行证</Option>
                  </Select>
                </FormItem>
              </Col>
            </Row>
            <Row :gutter="16">
              <Col span="5">
                <FormItem label="联系电话：" prop="tel">
                  <Input type="text" v-model="formInfo.tel" placeholder="请输入联系电话"/>
                </FormItem>
              </Col>
              <Col span="5">
                <FormItem label="职业：" prop="occupation">
                  <Select v-model="formInfo.occupation" placeholder="请选择职业" filterable>
                    <Option v-for="obj in occupationList" :value="obj.name" :key="obj.id">{{obj.name}}</Option>
                  </Select>
                </FormItem>
              </Col>
              <Col span="5">
                <FormItem label="重点关注类型：" prop="follow_type">
                  <Select v-model="formInfo.follow_type" placeholder="请选择重点关注类型" filterable>
                    <Option v-for="obj in follow_type_list" :value="obj.value" :key="obj.id">{{obj.label}}</Option>
                  </Select>
                </FormItem>
              </Col>
            </Row>
            <Row :gutter="16">
              <Col span="5">
                <FormItem prop="area1">
                  <template slot="label">
                    <span class="red-star">*</span>住址：
                    <Icon type="md-help-circle" class="help-tip" @click="showHelp(3)"/>
                  </template>
                  <Select v-model="formInfo.area1" placeholder="区/县" filterable @on-change="perChange(1, false)">
                    <Option v-for="obj in addressInfo.list1" :value="obj.title" :key="obj.id">{{obj.title}}</Option>
                  </Select>
                </FormItem>
              </Col>
              <Col span="5">
                <FormItem label="ㅤ" prop="area2">
                  <Select v-model="formInfo.area2" placeholder="街道" filterable @on-change="perChange(2, false)">
                    <Option v-for="obj in addressInfo.list2" :value="obj.title" :key="obj.id">{{obj.title}}</Option>
                  </Select>
                </FormItem>
              </Col>
              <Col span="5" >
                <FormItem label="ㅤ" prop="area3">
                  <Select v-model="formInfo.area3" placeholder="社区/村" filterable>
                    <Option v-for="obj in addressInfo.list3" :value="obj.title" :key="obj.id">{{obj.title}}</Option>
                  </Select>
                </FormItem>
              </Col>
              <Col span="9">
                <FormItem label="ㅤ" prop="address">
                  <Input type="text" v-model="formInfo.address" placeholder="具体地址"/>
                </FormItem>
              </Col>
            </Row>
            <div class="title-style">登记信息</div>
            <Row :gutter="16">
              <Col span="5">
                <FormItem prop="register_in_time">
                  <template slot="label">
                    <span class="red-star">*</span>进入社区登记时间：
                  </template>
                  <DatePicker type="date" placeholder="选择时间" style="width: 100%" v-model="formInfo.register_in_time"></DatePicker>
                </FormItem>
              </Col>
              <Col span="5">
                <FormItem prop="register_out_time">
                  <template slot="label">
                    <span class="red-star">*</span>离开社区登记时间：
                  </template>
                  <DatePicker type="date" placeholder="选择时间" style="width: 100%" v-model="formInfo.register_out_time"></DatePicker>
                </FormItem>
              </Col>
            </Row>
            <Row :gutter="16">
              <Col span="15">
                <FormItem label="备注信息：">
                  <Input type="textarea" v-model="formInfo.remark" :rows="3"/>
                </FormItem>
              </Col>
            </Row>
          </Form>
          <div slot="footer">
            <Button type="info" @click="savePersonInfo">保存</Button>
            <Button type="default" @click="cancel">关闭</Button>
          </div>
        </Modal>
        <Modal v-model="registerModal" title="登记情况" width="1000">
          <Table :loading="countData.loading" border :columns="columns1" :data="personRegisList" size="small" ref="table">
          </Table>
          <Row type="flex" justify="end" class="page_style">
            <Page :current="param1.pageNo" :total="param1.totalRecord" :page-size="param1.pageSize"
                  show-total @on-change="pageAction1"/>
          </Row>
          <!-- <div slot="footer">
            <Button type="info" @click="savePersonInfo">保存</Button>
            <Button type="default" @click="cancel">关闭</Button>
          </div> -->
        </Modal>
    </div>
</template>

<script>
import validateData from './validate'
import { getCityList } from '@/api/common'
import { findUserUpList, getDictList, saveUserInfo, getUserInfoByCardNo, findRegisterList } from '@/api/yi-qing-fang-kong'
import { grid_downlist, date2str } from '@/api/eventlist'
export default {
  name: 'registration-list',
  data () {
    return {
      superviseDictList: [],
      dictList: [],
      loadData: true,
      formInfo: {},
      loading: false,
      fcAllDataList: [],
      type: this.$route.query.type, // 列表类型
      addressInfo: { list1: [], list2: [], list3: [] },
      addressInfo1: { list1: [], list2: [], list3: [] },
      company_list: [],
      card_no: '',
      countData: { loading: false, fcnum: 0, ytbnum: 0, zcnum: 0 },
      params: { pageNo: 1, pageSize: 10, totalRecord: 0, company_code: '', name: '', card_no: '' },
      param1: { pageNo: 1, pageSize: 10, totalRecord: 0 },
      columns2: [
        { title: '姓名', key: 'name', width: 100, align: 'center', fixed: 'left' },
        { title: '性别', key: 'sex', width: 80, align: 'center' },
        { title: '证件类型', slot: 'card_type', width: 120, align: 'center' },
        { title: '证件号', key: 'card_no', width: 180, align: 'center' },
        { title: '联系方式', key: 'tel', width: 120, align: 'center' },
        { title: '重点关注类型', key: 'follow_type', width: 140, align: 'center' },
        { title: '职业', key: 'occupation', width: 100, align: 'center' },
        { title: '区县', key: 'area', width: 100, align: 'center' },
        { title: '街道', key: 'street', width: 100, align: 'center' },
        { title: '社区', key: 'community', width: 100, align: 'center' },
        { title: '具体地址', key: 'address', width: 100, align: 'center' },
        { title: '操作', align: 'center', width: 200, slot: 'action', fixed: 'right' }
      ],
      columns1: [
        { title: '序号', type: 'index', align: 'center' },
        { title: '进入社区登记时间', key: 'register_in_time', align: 'center' },
        { title: '离开社区登记时间', key: 'register_out_time', align: 'center' },
        { title: '登记社区名称', key: 'create_company_name', align: 'center' },
        { title: '备注', key: 'remark', align: 'center' }
      ],
      personRegisList: [],
      fcDataList: [],
      ruleValidate: validateData.ruleValidate,
      modal3: false,
      occupationList: validateData.formData.occupationList,
      follow_type_list: [],
      registerModal: false,
      inoutCity: { provinceList: [], city1: [], city2: [] },
      user_id: '',
      editFlag: true
    }
  },
  computed: {
    companyCode () {
      return this.$store.state.user.severUserInfo.company_code
    }
  },
  watch: {
    $route: function (to, from) {
      console.log(this.$route.query.type, to, '!@!@#@')
      this.type = to.query.type
      this.params.user_type = to.query.user_type
      console.log(this.params, '入参')
      this.selectlist()
    }
  },
  methods: {
    savePersonInfo () {
      this.$refs.addBaseForm.validate((valid) => {
        if (valid) {
          let param = {
            name: this.formInfo.name,
            sex: this.formInfo.sex,
            card_type: this.formInfo.card_type,
            card_no: this.formInfo.card_no,
            occupation: this.formInfo.occupation,
            tel: this.formInfo.tel,
            area: this.formInfo.area1,
            street: this.formInfo.area2,
            community: this.formInfo.area3,
            address: this.formInfo.address,
            register_in_time: date2str(this.formInfo.register_in_time),
            register_out_time: date2str(this.formInfo.register_out_time),
            follow_type: this.formInfo.follow_type,
            remark: this.formInfo.remark
          }
          this.createuser(param)
        } else {
          this.$Notice.warning({ title: '请查看表必填项!' })
        }
      })
    },
    createuser (params) { // 调用保存信息接口
      saveUserInfo(params).then(res => {
        if (res.data.errcode === 0) {
          this.$Notice.success({ title: '成功' })
          this.modal3 = false
          this.selectlist()
          return
        }
        this.$Notice.error({ title: res.data.errmsg })
      })
    },
    register () {
      this.editFlag = true
      this.formInfo = {}
      this.modal3 = true
    },
    pageSizeChange (e) {
      this.params.pageSize = e
      this.selectlist(this.params.pageNo)
    },
    // pageSizeChange1 (e) {
    //   console.log('fsafd')
    //   this.param1.pageSize = e
    //   this.getPerRegisList(this.param1.pageNo)
    // },
    pageAction (page) {
      this.params.pageNo = page
      this.getList()
    },
    pageAction1 (page) {
      this.param1.pageNo = page
      this.getPerRegisList()
    },
    selectlist () {
      this.params.pageNo = 1
      this.params.company_code = this.params.area3
      this.getList()
    },
    getList () {
      // ---调用接口可以打开
      this.fcDataList = []
      findUserUpList(this.params).then(res => {
        if (res.data.errcode === 0 || res.data.errcode === 1001) {
          this.fcDataList = res.data.data.results
          this.loadData = false
          this.countData.fcnum = res.data.data.totalRecord
          this.params.totalRecord = res.data.data.totalRecord
        } else {
          this.loadData = true
          this.$Notice.error({
            title: '查询列表信息失败',
            desc: res.data.errmsg
          })
        }
      })
    },
    showdetail (row) {
      this.registerModal = true
      this.user_id = row.user_id
      this.getPerRegisList()
    },
    getPerRegisList () { // 获取个人登记记录列表
      let param = {
        user_id: this.user_id,
        pageNo: this.param1.pageNo,
        pageSize: this.param1.pageSize
      }
      findRegisterList(param).then(res => {
        if (res.data.errcode === 0) {
          this.personRegisList = res.data.data.results
          this.param1.totalRecord = res.data.data.totalRecord
        } else {
          this.loadData = true
          this.$Notice.error({
            title: '查询列表信息失败',
            desc: res.data.errmsg
          })
        }
      })
    },
    cancel () {
      this.modal3 = false
    },
    findDictList (value) {
      let that = this
      var dic = { 'label': '全部', 'value': '' }
      getDictList({ type: 'arrive_address_type', pageSize: 30, pageNo: 1 }).then(res => {
        if (res.data.errcode === 0) {
          this.dictList = res.data.data.results
          setTimeout(function () {
            that.selectlist()
          }, 2000)
        } else {
          this.$Notice.error({ title: '查询失败' })
        }
      })
      getDictList({ type: 'supervise_start' }).then(res => {
        if (res.data.errcode === 0) {
          this.superviseDictList = res.data.data.results
          this.superviseDictList.unshift(dic)
          setTimeout(function () {
            that.selectlist()
          }, 500)
        } else {
          this.$Notice.error({ title: '查询失败' })
        }
      })
    },
    chaneProv (index) {
      let temparea = this.formInfo['province' + index]
      if (!temparea) {
        return
      }
      let templist = this.inoutCity.provinceList
      let tempdata = {}
      for (let i = 0; i < templist.length; i++) {
        if (temparea === templist[i].name) {
          tempdata = templist[i]
          break
        }
      }
      this.getCity(tempdata.code, 'city' + index, false)
    },
    getCity (code, listname, flag) {
      getCityList({ code: code }).then(res => {
        if (res.data.errcode === 0) {
          this.inoutCity[listname] = res.data.data
          if (flag) {
            if (this.formInfo.province1) {
              this.chaneProv(1)
            }
            if (this.formInfo.province2) {
              this.chaneProv(2)
            }
          }
        } else {
          this.$Notice.error({ title: '查询失败' })
        }
      })
    },
    getDictDataList () {
      this.follow_type_list = []
      let param = {
        pageNo: 1,
        pageSize: 60,
        type: 'follow_type'
      }
      getDictList(param).then(res => {
        if (res.data.errcode === 0) {
          console.log(res.data.data, '人员类型')
          this.follow_type_list = res.data.data.results
        } else {
          this.$Notice.error({ title: '查询失败' })
        }
      })
    },
    perChange (index, flag) {
      if (index === 3 || (this.company_type === '2' && index === 2)) {
        return
      }
      let temparea = this.formInfo['area' + index]
      if (!temparea) {
        return
      }
      let templist = this.addressInfo['list' + index]
      let tempdata = {}
      for (let i = 0; i < templist.length; i++) {
        if (temparea === templist[i].title) {
          tempdata = templist[i]
          break
        }
      }
      this.getDownAddress(tempdata.id, index + 1, flag)
    },
    perChange1 (index) {
      console.log(index)
      let temparea = this.params['area' + index]
      if (!temparea) {
        return
      }
      let templist = this.addressInfo1['list' + index]
      let tempdata = {}
      for (let i = 0; i < templist.length; i++) {
        if (temparea === templist[i].company_code) {
          tempdata = templist[i]
        }
      }
      this.getDownAddress1(tempdata.id, index + 1)
    },
    // 下拉搜索框的级联查询
    getDownAddress1 (pid, index) {
      console.log('#@@$@#$#@$@')
      let param = {
        pid: pid,
        pageNo: 1,
        pageSize: 200
      }
      grid_downlist(param).then(res => {
        if (res.data.data) {
          this.addressInfo1['list' + index] = res.data.data.results
          let obj = {
            id: '',
            level: '',
            mapjson: '',
            path: '',
            pid: 0,
            title: '全部',
            company_code: ''
          }
          this.addressInfo1['list' + index].unshift(obj)
          console.log(this.addressInfo1, '!!!!')
          this.perChange1(index)
        } else {
          this.$Notice.error({ title: '查看失败', desc: '查询地址信息失败' })
        }
      })
    },
    getDownAddress (pid, index, flag) {
      let param = {
        pid: pid,
        pageNo: 1,
        pageSize: 200
      }
      grid_downlist(param).then(res => {
        if (res.data.data) {
          this.addressInfo['list' + index] = res.data.data.results
          if (flag) {
            this.perChange(index, flag)
          }
        } else {
          this.$Notice.error({ title: '查看失败', desc: '查询地址信息失败' })
        }
      })
    },
    personRegister (row) {
      this.editFlag = false
      // this.card_no = row.card_no
      this.getUserInfo(row.card_no)
      this.modal3 = true
    },
    getUserInfo (card_no) {
      this.formInfo.area1 = ''
      this.formInfo.area2 = ''
      this.formInfo.area3 = ''
      getUserInfoByCardNo({ card_no: card_no }).then(res => {
        if (res.data.errcode === 0) {
          console.log(res.data.data, '人员信息')
          this.getCity(null, 'provinceList', true)
          this.getDownAddress(0, 1, true)
          this.formInfo = res.data.data
          this.formInfo.area1 = res.data.data.area
          this.formInfo.area2 = res.data.data.street
          this.formInfo.area3 = res.data.data.community
        } else if (res.data.errcode === 10003) {
          const errorTitle = '查询失败'
          const content = '<p>请完善个人信息</p>'
          this.$Modal.error({
            title: errorTitle,
            content: content
          })
        } else {
          this.$Notice.error({ title: '查询失败' })
        }
      })
    }
  },
  mounted () {
    this.params.user_type = this.$route.query.user_type
    console.log(this.$route.query.user_type, 'dfsfafsd')
    this.selectlist()
    this.getDictDataList()
    this.getDownAddress(0, 1)
    this.getDownAddress1(0, 1)
  }
}
</script>

<style scoped>
    .page_style {
        margin: 20px 5px;
    }

    .input-frame {
        width: 120px;
        margin-right: 10px;
    }

    .house-list > .group {
        background: white;
        padding: 10px;
        margin-top: 10px;
    }

    .house-list > .group > .item {
        border-bottom: 1px solid #f5f5f5;
        padding: 10px 0;
    }

    .house-list > .group > .item .top {
        display: flex;
        justify-content: space-between;
        margin-bottom: 5px;
    }

    .house-list > .group > .item .title {
        font-size: 14px;
        color: #333333;
    }

    .house-list > .group > .item .address {
        font-size: 12px;
        color: gray;
        display: flex;
        align-items: center;
    }

    .house-list > .group > .item .detail {
        font-size: 12px;
        color: dodgerblue;
    }

    .opt-area-02 {
        display: flex;
        align-items: center;
        flex-direction: column;
        border: 1px solid #abdcff;
        background-color: #f0faff;
        padding: 8px 16px 8px 16px;
        border-radius: 4px;
        margin-bottom: 10px;
    }

    .opt-area-02 .right-con {
        width: 100%;
        margin-top: 5px;
        display: flex;
    }

    .opt-area-02 .left-con {
        font-size: 14px;
        width: 100%;
    }

    .opt-area-02 .left-con span {
        font-weight: bold;
    }

    .opt-area-02 .right-con input {
        width: 150px;
        margin-right: 10px;
    }

    .opt-area-02 .right-con .input-frame {
        width: 200px;
        margin-right: 10px;
    }

    .table-action-btn {
        margin-right: 3px;
    }

    .container >>> .ivu-btn-small {
        height: 24px;
        padding: 0 5px;
        font-size: 12px;
        border-radius: 3px;
    }

    .btn-action {
        margin-bottom: 10px;
    }

    .mp10 {
        margin-right: 10px;
    }
    .title-style {
      padding: 8px 12px;
      background-color: #ecf8ff;
      border-radius: 4px;
      border-left: 5px solid #50bfff;
      margin: 15px 0;
      font-size: 16px;
      width: 100%;
    }
    .mb20 {
      margin-bottom: 20px;
    }
    .ivu-table-row {
      height: 57px;
    }
    .red-star {
      font-family: SimSun;
      color: #f00;
      margin-right: 4px;
    }
</style>
